# -- coding: utf-8 --
import time
import threading
from datetime import datetime
import requests


'''
最简单的例子
'''
# def begin_time(start,end):
#     for i in range(start,end):
#         time.sleep(1)
#         print(i)
#
#
# # target 方法名字 不要到括号 args 传参 需传一个元组就算参数只有一个也要带一个逗号
# t0 = threading.Thread(target=begin_time,args=(1,4))
# t1 = threading.Thread(target=begin_time,args=(101,104))
# t0.start()
# t1.start()
# t0.join()
# t1.join()
# # 加上join就一步一步来

'''
串行and并行 访问几个网页
'''

# 串行

# def fetch(url):
#     response=requests.get(url)
#     print('GET %s: %s' % (url,response))
#
# time0 = datetime.now()
#
# fetch('https://www.baidu.com/')
# fetch('https://www.weibo.com/')
# fetch('https://www.zhihu.com/')
#
# time1 = datetime.now()
#
# time = time1 - time0
#
# print(time.microseconds)

# 554147

# 并行

def fetch(url):
     response=requests.get(url)
     print('GET %s: %s' % (url,response))

time0 = datetime.now()

t0 = threading.Thread(target=fetch,args=("https://www.baidu.com/",))
t1 = threading.Thread(target=fetch,args=("https://www.weibo.com/",))
t2 = threading.Thread(target=fetch,args=("https://www.qq.com/",))

t0.start()
t1.start()
t2.start()
t0.join()
t1.join()
t2.join()

time1= datetime.now()
endtime = time1 - time0
print(endtime.microseconds)


